const fs = require("fs");
const { DxfParser, toGeojson } = require("dxf2json");
const path = require('path')

/**
 * 保存 dxf 成 json
 * @param {String} filePath 要读取的文件路径
 * @param {String} savePath 要保存的文件路径
 * @returns {JSON} 内容
 */
export function saveAsJSONFile(filePath, savePath) {
  const content = fs.readFileSync(filePath, "utf-8");
  return new DxfParser().parseContent(content).then((result) => {
    const json = toGeojson(result);
    const content = JSON.stringify(json, null, 2);
    fs.writeFileSync(savePath, content);
    return content;
  });
}

export function getLineStringFeatures(arr) {
  return arr.filter((i) => i.geometry && i.geometry.type === "LineString");
}

export function transform(coordinates) {
  if (coordinates.filter(Boolean).length === 3) {
    return proj4(firstProjection, secondProjection, coordinates);
  }
}